#! /usr/bin/python
# -*- coding: cp1250 -*-

import re, string, glob

def extract_tiddler_name(text):
    """ 
    example : <div title="MarkupPreHead"><pre
    returns : MarkupPreHead
    """
    name = re.sub('\".*', '', text[12:])
    return name

class ExamineTw:
    """ """
    def __init__(self):
        """ """
        pass
        
    def load_tw(self, inputfile=''):
        """ """
        self.tw_data = ""
        self.tw_file = ""
        self.tw_tiddlers = {} # tiddlername : {'name': ..., 'size': ..., 'body': ...}
        self.tdid = 0

        self.tw_file = inputfile
        inpf = open(self.tw_file,'r')
        self.tw_data = inpf.read()
        inpf.close()

        self.tw_data = re.sub('\n', '', self.tw_data)
        self.tw_data = re.sub('\<div title=', '\n<div title=', self.tw_data)
        self.tw_data = re.sub('\<!--POST-STOREAREA', '\n<!--POST-STOREAREA', self.tw_data)
        
        for t in string.split(self.tw_data, '\n'):
            if t[0:11] == '<div title=':
                tname = extract_tiddler_name(t)
                tsize = len(t[12:])
                self.tdid += 1; tdid = string.zfill(self.tdid, 4)
                self.tw_tiddlers[tdid] = {'id': tdid, 'name': tname, 'size': tsize}
        return self.tw_tiddlers

    def list_tiddlers(self):
        ts = self.tw_tiddlers
        for id in sorted(ts.keys()):
            print ts[id]['id'], ts[id]['name'], ts[id]['size']

class CompareTw:
    """ Compare two TiddlyWiki files
    
    # examine tw A
    # examine tw B
    # provide routines to compare A and B """

    def __init__(self, a, b):
        """ """
        self.afile = a
        self.bfile = b
        eta = ExamineTw()
        self.ainfo = eta.load_tw(a)
        etb = ExamineTw()
        self.binfo = etb.load_tw(b)

        self.a_namesize = {}
        for id in self.ainfo.keys():
            name = self.ainfo[id]['name']
            size = self.ainfo[id]['size']
            self.a_namesize[name] = size

        self.b_namesize = {}
        for id in self.binfo.keys():
            name = self.binfo[id]['name']
            size = self.binfo[id]['size']
            self.b_namesize[name] = size


    def list_equal_namesize(self):
        """ """
        res = {}
        for aname in self.a_namesize.keys():
            if self.b_namesize.has_key(aname):
                if self.b_namesize[aname] == self.a_namesize[aname]:
                    res[aname] = self.a_namesize[aname]

        for rk in sorted(res.keys()):
            print "[[%s]] %s" % (rk, res[rk])

    def list_unequal_namesize(self):
        """ """
        res = {}
        for aname in self.a_namesize.keys():
            if self.b_namesize.has_key(aname):
                if self.b_namesize[aname] <> self.a_namesize[aname]:
                    res[aname] = [self.a_namesize[aname], self.b_namesize[aname]]

        for rk in sorted(res.keys()):
            print "[[%s]] %s %s - > %s %s" % (rk, self.afile, res[rk][0], self.bfile, res[rk][1])

def list_tiddlers(inputfile):
    """ """
    t = ExamineTw()
    t.load_tw(inputfile=r'a.html')
    t.list_tiddlers()

def compare_tw(a, b):
    """ """
    tc = CompareTw(a,b)

    print '\n\n :: EQUAL TIDDLERS >> \n'
    tc.list_equal_namesize()

    print '\n\n :: UNEQUAL TIDDLERS >> \n'
    tc.list_unequal_namesize()

if __name__ == "__main__":
    """ """
    a, b = glob.glob('*.html')
    compare_tw(a, b)
